<template>
  <div class="warpper">
    <div v-show="flag" class="login-content animate__animated animate__fadeIn">
      <h2 class="title">活动信息发布管理系统</h2>
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item prop="uid">
          <el-input prefix-icon="el-icon-user" class="login-name" placeholder="请输入账号" v-model="form.uid"> </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" show-password class="login-password" type="password" v-model="form.password" placeholder="请输入密码"> </el-input>
        </el-form-item>
        <el-button class="login-btn" :loading="loginLoa" type="primary" @click="submitForm('ruleForm')">登 录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/loginApi.js'
import { setTrendsRouter } from '../../router'

export default {
  data() {
    return {
      loginLoa: false,
      flag: false,
      form: {
        uid: '20200216119',
        password: '123456',
      },
      rules: {
        uid: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 4, max: 16, message: '长度不符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3~16 个字符之间', trigger: 'blur' },
        ],
      },
    }
  },
  created() {
    this.flag = true
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loginLoa = true
          login(this.form)
            .then((res) => {
              if (res.data.code != '200') {
                this.loginLoa = false
                return
              }
              //将用户信息存储到浏览器缓存中
              this.$sessionStorage('set', 'userInfo', res.data.data)
              this.$sessionStorage('set', 'token', res.data.data.token)
              setTrendsRouter()
              this.$message.success('登录成功')
              setTimeout(() => {
                this.$router.push('/home/statistics')
              }, 1000)
            })
            .catch((err) => {
              return (this.loginLoa = false)
            })
        } else {
          return false
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.warpper {
  color: #fff;
  width: 100%;
  height: 100%;
  background: url('../../assets/login_bg.jpg');
  background-size: 100% 100%;
  .login-content {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 400px;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    flex-direction: column;
    border-radius: 10px;
    padding: 50px 20px;
    .title {
      margin-bottom: 25px;
      font-family: '阿里妈妈刀隶体 Regular';
      font-size: 36px;
      color: #409eff;
    }
    .el-form {
      height: 100%;
      .login-btn {
        width: 100%;
        font-weight: 550;
        font-size: 18px;
      }
    }
    .el-form .el-input {
      width: 300px;
    }
  }
}
</style>